/* 
 * File:   Label.cpp
 * Author: lgr013
 * 
 * Created on 13 de octubre de 2014, 13:19
 */

#include "Label.h"
//SBSLog* myLogFile;

Label::Label() {
    this->labelText = "";
    this->s.pathToTTF = "Fonts/emulogic.ttf";
    this->s.fontSize = 14;
    this->s.textColor = {0, 0, 0, 0xFF};
    this->tWidth = 0;
    this->tHeight = 0;
    this->myFont = NULL;
//    myLogFile->getInstance(MY_LOG_MODE);
}

Label::~Label() {
    TTF_Quit();
}

void Label::setLabelText(string etiqueta) {
    this->labelText = etiqueta;
}

void Label::setLabelStyle(char* pathFuente, int size, SDL_Color color) {
    this->s.pathToTTF = pathFuente;
    this->s.fontSize = size;
    this->s.textColor = color;
}

TTF_Font* Label::getFontType() {
    TTF_Font* gFont = NULL;
    gFont = this->myFont;
    return gFont;
}

bool Label::initLabelFont() {
    //Loading success flag
    //cout<<"load font type"<<endl;
    bool success = true;
    TTF_Font *gFont = NULL;
    //Open the font
    gFont = TTF_OpenFont(this->s.pathToTTF, this->s.fontSize);
    //    cout<<this->s.pathToTTF<<endl;
    //    cout<<this->s.fontSize<<endl;
    if (gFont == NULL) {
        //        cout<<"Era el pxxxxx esteeeeeeeeeeeeeeeeeeeeeeee"<<endl;
        //        printf( "Failed to load font! SDL_ttf Error: %s\n", TTF_GetError() );
//        myLogFile->writeInLog(2, "[2] No se pudo cargar la fuente de los labels.\n");
        success = false;
    } else {
        this->myFont = gFont;
        //cout<<"entre al else gFont <> NULL"<<endl;
    }
//    myLogFile->writeInLog(0, "[0] Se inicializarono correctamente las fonts de las labels.\n");
    return success;
}

int Label::getWidht() {
    int ancho;
    ancho = this->tWidth;
    return ancho;
}

int Label::getHeight() {
    int alto;
    alto = this->tHeight;
    return alto;
}

string Label::getLabelText() {
    string etiqueta;
    etiqueta = this->labelText;
    return etiqueta;
}

bool Label::createLabelTexture(SDL_Renderer** gRenderer) {

    bool success = true;
    if (this->myFont == NULL) {
        this->initLabelFont();
    }
    //Render text surface
    if (this->labelText.size() != 0) {
        SDL_Surface* textSurface = TTF_RenderText_Solid(this->myFont, this->labelText.c_str(), this->s.textColor);
        //        cout<< this->labelText<<endl;
        if (textSurface != NULL) {
            SDL_Texture* mTexture;
            //Create texture from surface pixels

            mTexture = SDL_CreateTextureFromSurface(*gRenderer, textSurface);
            if (mTexture == NULL) {
                //                        printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
//                myLogFile->writeInLog(2, "[2] No se puede renderear el texto de las labels.\n");

                success = false;
            } else {
                //                cout << "textura ok" << endl;
                //Get image dimensions
                tTexture = mTexture;
                tWidth = textSurface->w;
                tHeight = textSurface->h;
            }

            //Get rid of old surface
            SDL_FreeSurface(textSurface);

        } else {
            //            cout << "esteeeeeeeeeeeeee" << endl;
            printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
            //            cout << "esteeeeeeeeeeeeee" << endl;
            success = false;
        }
    }
    //Return success
    return success;
}

void Label::renderLabel(SDL_Renderer** gRenderer, int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip) {
    //Set rendering space and render to screen
    SDL_Rect renderQuad = {x, y, this->tWidth, this->tHeight};

    //Set clip rendering dimensions
    if (clip != NULL) {
        renderQuad.w = clip->w;
        renderQuad.h = clip->h;
    }

    //Render to screen
    SDL_RenderCopyEx(*gRenderer, this->tTexture, clip, &renderQuad, angle, center, flip);
}

bool Label::handleEvent(SDL_Event* event) {
    bool success = false;
    //Special key input
    if (event->type == SDL_KEYDOWN) {
        //Handle backspace
        if (event->key.keysym.sym == SDLK_BACKSPACE && (this->labelText.length() > 0)) {
            //lop off character
            //            cout << "ANTES" << this->labelText.length() << endl;
            this->labelText.pop_back();
            //            cout << "borre un caracter" << endl;
            //            cout << "DESPUES" << this->labelText.length() << endl;
            success = true;
        }//Handle copy
        else if (event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL) {
            SDL_SetClipboardText(this->labelText.c_str());
        }//Handle paste
        else if (event->key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL) {
            this->labelText = SDL_GetClipboardText();
            success = true;
        }

    }//Special text input event
    else if (event->type == SDL_TEXTINPUT) {
        //Not copy or pasting
        if (!((event->text.text[ 0 ] == 'c' || event->text.text[ 0 ] == 'C') && (event->text.text[ 0 ] == 'v' || event->text.text[ 0 ] == 'V') && SDL_GetModState() & KMOD_CTRL)) {
            //Append character
            this->labelText += event->text.text;
            success = true;
        }
    }
    return success;
}

void Label::printLabel(char* pathFuente, int size, SDL_Color color, SDL_Renderer** gRenderer, int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip) {

    setLabelStyle(pathFuente, size, color);
    createLabelTexture(gRenderer);
    renderLabel(gRenderer, x, y, clip, angle, center, flip);
}